package com.example.seakdreamandroid.gentleMusic;

import static com.mob.MobSDK.getContext;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.seakdreamandroid.MusicStoryPlayerActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.entity.User;

import java.util.List;

public class GridViewMethod extends BaseAdapter {
    User user;
    private Context context;
    private int layoutId;
    private List<com.example.seakdreamandroid.gentleMusic.Audio> Audio;

    public GridViewMethod(Context context, int layoutId, List<com.example.seakdreamandroid.gentleMusic.Audio> Audio){
        this.context=context;
        this.layoutId=layoutId;
        this.Audio = Audio;
    }
    public int getCount() {
        return Audio.size();
    }

    @Override
    public Object getItem(int i) {
        return Audio.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.item,null);
        }
        ImageView imageView = view.findViewById(R.id.image);
        TextView text1 = view.findViewById(R.id.text1);
        TextView text2 = view.findViewById(R.id.text2);
        TextView text3 = view.findViewById(R.id.text3);
        View ceshi = view.findViewById(R.id.ceshi);
        user = (User)getContext().getApplicationContext();

        com.example.seakdreamandroid.gentleMusic.Audio audio1 = Audio.get(i);
        Glide.with(context).load(audio1.getImage()).into(imageView);
        if(audio1.getIf_vip().equals("1")){
            text1.setText(audio1.getText1()+"(vip)");
            text1.setTextColor(Color.RED);
        }else{
            text1.setText(audio1.getText1());
            text1.setTextColor(Color.BLACK);
        }
        text2.setText(audio1.getText2());

        ceshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (audio1.getIf_vip().equals("1")){
                    if(user.getIf_vip().equals("1")){
                        Intent intent = new Intent();
                        intent.setClass(context, MusicStoryPlayerActivity.class);
                        intent.putExtra("audioUrl",audio1.getUrl());
                        intent.putExtra("audioId",audio1.getId());
                        intent.putExtra("audioName", audio1.getText1());
                        intent.putExtra("audioAuthor", audio1.getText2());
                        context.startActivity(intent);
                    }else {
                        Toast.makeText(context, "请充值vip", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Intent intent = new Intent();
                    intent.setClass(context, MusicStoryPlayerActivity.class);
                    intent.putExtra("audioUrl",audio1.getUrl());
                    intent.putExtra("audioId",audio1.getId());
                    intent.putExtra("audioName", audio1.getText1());
                    intent.putExtra("audioAuthor", audio1.getText2());
                    context.startActivity(intent);
                }

            }
        });
        return view;
    }
}





